package com.hlf.modules.api.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.service.StoreService;
import com.hlf.modules.sys.entity.SoftService;
import com.hlf.modules.sys.entity.SysUserEntity;
import com.hlf.modules.sys.service.SoftwareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 软件服务
 * Created by Administrator on 2017/10/31.
 */
@RestController
@RequestMapping("/api/soft")
@Api("软件服务接口")
public class ApiSoftController {

    @Autowired
    private SoftwareService softwareService;
    @Autowired
    private StoreService storeService;

    /**
     * 服务商信息
     */
    @GetMapping("info")
    @ApiOperation(value = "服务商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult info(@LoginUser SysUserEntity user) {
        long storeId = Long.parseLong(user.getCurrentStoreId());
        Store store = storeService.queryObject(storeId);
        SoftService info = softwareService.getInfo();
        info.setCurrentStoreName(store.getName());
        info.setSoftwareStatus(store.getSoftwareStatus());
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,info);
    }

    /**
     * 购买服务
     */
    @SysLog("购买软件服务")
    @PostMapping("buy")
    @ApiOperation(value = "购买服务", notes = "购买服务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Byte", name = "serviceYearNum", value = "购买服务年限", required = true)
    })
    public ReturnResult buy(@LoginUser SysUserEntity user, Integer serviceYearNum) {
        Assert.isNull(serviceYearNum, "购买服务年限不能为空!");
        softwareService.deleteUnfinish(user);
        return softwareService.buy(user,serviceYearNum);
    }


}
